package ExamSystemClient.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 实现客户端的初始化和关闭操作
 *
 * @author Shuxian Huang
 */
public class ClientInitClose {
    /**
     * 自定义成员变量记录Socket和流对象
     */
    private Socket s;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;

    public ObjectOutputStream getOos(){
        return oos;
    }

    public ObjectInputStream getOis(){
        return ois;
    }

    /**
     * 实现客户端的初始化操作
     * @throws IOException
     */
    public void clientInit() throws IOException {
        //1、创建Socket类型的对象并指定多个服务器的通信地址和端口号
        s = new Socket(InetAddress.getLocalHost(), 9999);
        //2、使用输入输出流通信
        oos = new ObjectOutputStream(s.getOutputStream());
        ois = new ObjectInputStream(s.getInputStream());
        System.out.println("连接服务器成功！");
    }

    /**
     * 实现客户端的关闭操作
     * @throws IOException
     */
    public void clientClose() throws IOException {
        ois.close();
        oos.close();
        s.close();
        System.out.println("客户端成功关闭！");
    }


}
